package cn.com.jonpad.demo.advice;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.support.WebExchangeBindException;

/**
 * @author Jon Chan
 * @date 2019/5/14 22:36
 */
@ControllerAdvice
public class ChackAdvice {
    @ExceptionHandler(WebExchangeBindException.class)
    public ResponseEntity<String> handleExceptionHandler(WebExchangeBindException e){
        String reduce = e.getFieldErrors().stream()
                .map(ex -> ex.getField() + ":" + ex.getDefaultMessage())
                .reduce("", (s1, s2) -> s1 + "\n" + s2);
        return new ResponseEntity<>(reduce, HttpStatus.BAD_REQUEST);
    }
}
